﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.IO;
public partial class Main_ReadXML : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void writeData()
    {
        string date = System.DateTime.Now.ToString(string.Format("dd-MMM-yyyy"));
        string path = HttpContext.Current.Server.MapPath("~/XML/");
        string fileName = string.Empty;
        string FileNames = path + "Log_" + date + ".xml";
        DirectoryInfo dir = new DirectoryInfo(path);
        FileInfo[] fileList = dir.GetFiles("Log_*.xml", System.IO.SearchOption.AllDirectories);
        if (fileList.LongLength == 0)
        {
            XmlTextWriter xmlWriter = new XmlTextWriter(FileNames, System.Text.Encoding.UTF8);
            xmlWriter.Formatting = Formatting.Indented;
            xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
            xmlWriter.WriteStartElement("LogsDetails");
            xmlWriter.Close();

            XmlDocument xmldoc = new XmlDocument();
            //xmldoc.Load(Server.MapPath("~/XML/EmployeEdit" + DateTime.Now.ToShortDateString() + ".xml"));
            //xmldoc.Load("c:\\firstXML" + DateTime.Now.ToString(string.Format("dd-MMM-yyyy")) + ".xml");
            xmldoc.Load(FileNames);
            XmlElement parentNode = xmldoc.CreateElement("log");

            XmlElement id = xmldoc.CreateElement("id");
            XmlElement userid = xmldoc.CreateElement("userid");
            XmlElement logtype = xmldoc.CreateElement("logtype");
            XmlElement logmesg = xmldoc.CreateElement("logmesg");
            XmlElement pagename = xmldoc.CreateElement("pagename");
            XmlElement ipaddr = xmldoc.CreateElement("ipaddr");
            XmlElement datetime = xmldoc.CreateElement("datetime");

            id.InnerText = "555";
            userid.InnerText = "Gautam";
            logtype.InnerText = "Opkl";
            logmesg.InnerText = "Subscribers must synchronize with the Publisher to receive new ranges. Because Subscribers are assigned identity ranges automatically, it is possible for any Subscriber to exhaust the entire supply of identity ranges if it repeatedly requests new ranges.";
            pagename.InnerText = "index";
            ipaddr.InnerText = "10.10.10.1";
            datetime.InnerText = DateTime.Now.ToString();

            parentNode.AppendChild(id);
            parentNode.AppendChild(userid);
            parentNode.AppendChild(logtype);
            parentNode.AppendChild(logmesg);
            parentNode.AppendChild(pagename);
            parentNode.AppendChild(ipaddr);
            parentNode.AppendChild(datetime);

            xmldoc.DocumentElement.AppendChild(parentNode);
            xmldoc.Save(FileNames);
        }
        else
        {
            var fileCount = from fileNew in fileList where fileNew.Name.Equals("Log_" + date + ".xml") select fileNew.Name;
            if (fileCount.Count() == 1)
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(FileNames);
                XmlElement parentNode = xmldoc.CreateElement("log");

                XmlElement id = xmldoc.CreateElement("id");
                XmlElement userid = xmldoc.CreateElement("userid");
                XmlElement logtype = xmldoc.CreateElement("logtype");
                XmlElement logmesg = xmldoc.CreateElement("logmesg");
                XmlElement pagename = xmldoc.CreateElement("pagename");
                XmlElement ipaddr = xmldoc.CreateElement("ipaddr");
                XmlElement datetime = xmldoc.CreateElement("datetime");

                id.InnerText = "555";
                userid.InnerText = "Gautam";
                logtype.InnerText = "Opkl";
                logmesg.InnerText = "Subscribers must synchronize with the Publisher to receive new ranges. Because Subscribers are assigned identity ranges automatically, it is possible for any Subscriber to exhaust the entire supply of identity ranges if it repeatedly requests new ranges.";
                pagename.InnerText = "index";
                ipaddr.InnerText = "10.10.10.1";
                datetime.InnerText = DateTime.Now.ToString();

                parentNode.AppendChild(id);
                parentNode.AppendChild(userid);
                parentNode.AppendChild(logtype);
                parentNode.AppendChild(logmesg);
                parentNode.AppendChild(pagename);
                parentNode.AppendChild(ipaddr);
                parentNode.AppendChild(datetime);

                xmldoc.DocumentElement.AppendChild(parentNode);
                xmldoc.Save(FileNames);
            }
            else
            {
                XmlTextWriter xmlWriter = new XmlTextWriter(FileNames, System.Text.Encoding.UTF8);
                xmlWriter.Formatting = Formatting.Indented;
                xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                xmlWriter.WriteStartElement("LogsDetails");
                xmlWriter.Close();

                XmlDocument xmldoc = new XmlDocument();
                //xmldoc.Load(Server.MapPath("~/XML/EmployeEdit" + DateTime.Now.ToShortDateString() + ".xml"));
                //xmldoc.Load("c:\\firstXML" + DateTime.Now.ToString(string.Format("dd-MMM-yyyy")) + ".xml");
                xmldoc.Load(FileNames);
                XmlElement parentNode = xmldoc.CreateElement("log");

                XmlElement id = xmldoc.CreateElement("id");
                XmlElement userid = xmldoc.CreateElement("userid");
                XmlElement logtype = xmldoc.CreateElement("logtype");
                XmlElement logmesg = xmldoc.CreateElement("logmesg");
                XmlElement pagename = xmldoc.CreateElement("pagename");
                XmlElement ipaddr = xmldoc.CreateElement("ipaddr");
                XmlElement datetime = xmldoc.CreateElement("datetime");

                id.InnerText = "555";
                userid.InnerText = "Gautam";
                logtype.InnerText = "Opkl";
                logmesg.InnerText = "Subscribers must synchronize with the Publisher to receive new ranges. Because Subscribers are assigned identity ranges automatically, it is possible for any Subscriber to exhaust the entire supply of identity ranges if it repeatedly requests new ranges.";
                pagename.InnerText = "index";
                ipaddr.InnerText = "10.10.10.1";
                datetime.InnerText = DateTime.Now.ToString();

                parentNode.AppendChild(id);
                parentNode.AppendChild(userid);
                parentNode.AppendChild(logtype);
                parentNode.AppendChild(logmesg);
                parentNode.AppendChild(pagename);
                parentNode.AppendChild(ipaddr);
                parentNode.AppendChild(datetime);

                xmldoc.DocumentElement.AppendChild(parentNode);
                xmldoc.Save(FileNames);
            }
        }
    }
    protected void btnXmlwrite_Click(object sender, EventArgs e)
    {
        writeData();        
    }
}

